package cn.dnui_xcz826.dnuifood_16110100826.controller;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.dnui_xcz826.dnuifood_16110100826.R;
import cn.dnui_xcz826.dnuifood_16110100826.adapter.FoodInfoAdapter;
import cn.dnui_xcz826.dnuifood_16110100826.bean.CollectBean;
import cn.dnui_xcz826.dnuifood_16110100826.bean.CommentBean;
import cn.dnui_xcz826.dnuifood_16110100826.bean.FoodBean;
import cn.dnui_xcz826.dnuifood_16110100826.bean.ResultBean;
import cn.dnui_xcz826.dnuifood_16110100826.listener.onRetrofitListener;
import cn.dnui_xcz826.dnuifood_16110100826.model.CollectModel;
import cn.dnui_xcz826.dnuifood_16110100826.model.CommentModel;
import cn.dnui_xcz826.dnuifood_16110100826.model.FoodModel;


public class FoodInfoActivity extends BaseActivity implements onRetrofitListener {
    private Context context;
    private String food_id = "", result, phonenum = "";
    private TextView foodinfo_intro, foodinfo_price, foodinfo_bottom_collect,foodinfo_foodname;
    private ImageView foodinfo_pic, foodinfo_return, foodinfo_collect, foodinfo_call;
    private Button foodinfo_purchase;
    private RecyclerView recyclerView;
    private LinearLayoutManager layoutManager;
    private FoodInfoAdapter adapter;

    //声明界面中需要的所有控件 View
    void initViews() {
        setLayout(R.layout.activity_food_info);
        context = FoodInfoActivity.this;
        //初始化界面控件 View
        foodinfo_return = findViewById(R.id.foodinfo_back);
        foodinfo_collect = findViewById(R.id.foodinfo_collect);
        foodinfo_purchase = findViewById(R.id.foodinfo_purchase);
        foodinfo_call = findViewById(R.id.foodinfo_call);
        foodinfo_intro = findViewById(R.id.foodinfo_intro);
        foodinfo_price = findViewById(R.id.foodinfo_price);
        foodinfo_foodname=findViewById(R.id.foodinfo_foodname);
        foodinfo_bottom_collect = findViewById(R.id.foodinfo_bottom_collect);
        foodinfo_pic = findViewById(R.id.foodinfo_pic);

        recyclerView = findViewById(R.id.recyclerview);
        layoutManager=new LinearLayoutManager(context);
        recyclerView.setLayoutManager(layoutManager);
        //每个item如果是确定高度，设置此项提高性能
        recyclerView.setHasFixedSize(true);
    }

    void initEvents() {
        //处理【返回】、【收藏】、【立即购买】和【联系商家】按钮的点击事件
        //【返回】按钮调用 finish()关闭当前界面
        //【收藏】按钮调用 collect()方法
        //【立即购买】按钮打开购买界面
        //【联系商家】按钮打开打电话界面
        FoodBean foodinfo=new FoodBean();
        foodinfo_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        foodinfo_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                collect();
            }
        });
        foodinfo_purchase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent intent = new Intent(FoodInfoActivity.this, PurchaseActivity.class);
//                intent.putExtra("shopid", foodinfo.getShop_id());
//                intent.putExtra("foodid", foodinfo.getFood_id());
//                intent.putExtra("foodname", foodinfo.getFoodname());
//                intent.putExtra("price", foodinfo.getPrice());
//                startActivity(intent);
            }
        });
        foodinfo_call.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onClick(View v) {
                phonenum = getIntent().getStringExtra(phonenum);
                Uri uri = Uri.parse("tel:" + phonenum);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                if (checkSelfPermission(Manifest.permission.CALL_PHONE) !=
                        PackageManager.PERMISSION_GRANTED) {
                    startActivity(intent);
                    return;
                }
            }
        });
    }

    void initDatas() {
        food_id = getIntent().getStringExtra("food_id");
        showFoodInfo(food_id);
        showComments();
        iscollected();
        //通过 Intent 获取菜谱 id
        //获取菜谱的详细信息，调用 showFoodInfo()方法
        //获取菜谱的评论列表，调用 showComments()方法
        //获取菜谱的收藏状态，调用 iscollected()方法
    }


    //获取菜谱的详细信息
    private void showFoodInfo(String food_id) {
        FoodModel foodModel = new FoodModel();
        foodModel.getFoodById(food_id, this);
    }


    //获取菜谱的评论列表
    private void showComments() {
        CommentModel commentModel=new CommentModel();
        commentModel.getAllCommentsByFood(food_id, this);
    }


    //判断菜谱收藏状态
    private void iscollected() {
        CollectModel collectModel = new CollectModel();
        collectModel.isCollected(getUser_id(), food_id, "1", this);
    }


    //对菜谱的收藏信息进行修改
    private void collect() {
        CollectModel collectModel=new CollectModel();
        collectModel.userCollectFood(getUser_id(), food_id, this);
    }


    @Override
    public void onSuccess(Object o, int flag) {
        switch (flag) {
            case Constants.GETFOODBYID://getFoodById
                FoodBean foodinfo = (FoodBean) o;
                if (!foodinfo.getPic().equals("")) {
                    Picasso.with(context).load(Constants.BASE_URL + foodinfo.getPic()).into(foodinfo_pic);
                }
                if(foodinfo.getFoodname()==null)
                {
                    foodinfo_foodname.setText("null");
                }
                else {
                    foodinfo_foodname.setText(foodinfo.getFoodname());
                }
                foodinfo_intro.setText("简介：" + foodinfo.getIntro());
                foodinfo_price.setText("￥" + foodinfo.getPrice() + "元");
                if (!foodinfo.getPic().equals("")) {
                    Picasso.with(context).load(Constants.BASE_URL + foodinfo.getPic()).into(foodinfo_pic);
                }
                break;

            case Constants.GETALLUSERCOMMENT://getAllUserComment
                adapter = new FoodInfoAdapter(FoodInfoActivity.this, (List<CommentBean>) o, R.layout.foodinfo_comment_item);
                recyclerView.setAdapter(adapter);
                break;

            case Constants.ISCOLLECTED://isCollected
                CollectBean collectStatusBean=(CollectBean) o;
                result=collectStatusBean.getCollected().toString();
                if (result.equals("0")) {
                    foodinfo_collect.setImageResource(R.drawable.ic_uncollection);
                    foodinfo_bottom_collect.setText("收藏");
                    foodinfo_bottom_collect.setTextColor(Color.parseColor("#000000"));
                }
                else {
                    foodinfo_collect.setImageResource(R.drawable.ic_collection);
                    foodinfo_bottom_collect.setText("取消收藏");
                    foodinfo_bottom_collect.setTextColor(Color.parseColor("#E53EA0"));
                }
                break;

            case Constants.USERCOLLECTFOOD://userCollectFood
                if (((ResultBean)o).getSuccess().equals("1")) {
                    if (result.equals("0")) {
                        foodinfo_collect.setImageResource(R.drawable.ic_collection);
                        foodinfo_bottom_collect.setText("取消收藏");
                        foodinfo_bottom_collect.setTextColor(Color.parseColor("#E53EA0"));
                        Toast.makeText(FoodInfoActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
                        result = "1";
                    } else {
                        foodinfo_collect.setImageResource(R.drawable.ic_uncollection);
                        foodinfo_bottom_collect.setText("收藏");
                        foodinfo_bottom_collect.setTextColor(Color.parseColor("#000000"));
                        Toast.makeText(FoodInfoActivity.this, "取消收藏成功", Toast.LENGTH_SHORT).show();
                        result = "0";
                    }
                }
                else {
                    Toast.makeText(FoodInfoActivity.this, "操作失败", Toast.LENGTH_SHORT).show();
                }
                break;
            }
        }

//    @Override
//    public void onSuccess(Object object, int flag) {
//        adapter = new FoodInfoAdapter(FoodInfoActivity.this, (List<CommentBean>)object, R.layout.foodinfo_comment_item);
//        recyclerView.setAdapter(adapter);
//    }

    @Override
        public void onFailure (String msg){
            Toast.makeText(FoodInfoActivity.this, "网络发生错误", Toast.LENGTH_SHORT).show();
            System.out.println("----msg="+msg);
        }

    }
